//
//  AddViewController.m
//  UI_通讯_26
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 liwenhan. All rights reserved.
//

#import "AddViewController.h"
#import "NewView.h"
#import "CoreDataManager.h"
#import "Persen.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface AddViewController ()

@property (nonatomic, strong)NewView *addNewView;

@property (nonatomic, strong)UILabel *titleLabel;

@property (nonatomic, strong)UIButton *leftButton;
@property (nonatomic, strong)UIButton *rightButton;

@property (nonatomic, strong)UIView *naviView;

@property (nonatomic, strong)CoreDataManager *manager;

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建数据管理器(单例)
    self.manager = [CoreDataManager defaults];
    NSLog(@"url ====  %@", [self.manager applicationDocumentsDirectory]);

    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.naviView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
    [self.view addSubview:self.naviView];
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 20, WIDTH - 200, 44)];
    [self.naviView addSubview:self.titleLabel];
    self.titleLabel.text = @"新联系人";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.naviView addSubview:self.leftButton];
    self.leftButton.frame = CGRectMake(0, 20, 60, 44);
    [self.leftButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:
     UIControlEventTouchUpInside];
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.naviView addSubview:self.rightButton];
    self.rightButton.frame = CGRectMake(WIDTH - 60, 20, 60, 44);
    [self.rightButton setTitle:@"完成" forState:UIControlStateNormal];
    [self.rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    

    self.addNewView = [[NewView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT)];
    [self.view addSubview:self.addNewView];
    
}

//  取消按钮
- (void)leftButtonAction:(UIButton *)button
{
    //返回给上一页
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//  保存按钮
- (void)rightButtonAction:(UIButton *)button
{
    // 创建实体描述
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Persen" inManagedObjectContext:self.manager.managedObjectContext];
    // 创建实体
    Persen *person = [[Persen alloc]initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    person.name = self.addNewView.nameField.text;
    person.number = self.addNewView.numberField.text;
    person.phone = self.addNewView.phoneField.text;
    person.qq = self.addNewView.qqField.text;
    person.city = self.addNewView.cityField.text;
    
    [self.manager saveContext];
 
    
  
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
